''' 留学：武当、桃花 '''
import functools, asyncio, cn2an
from pymud import Alias, Trigger, SimpleCommand, SimpleTrigger, SimpleAlias, Timer
from prompt_toolkit.mouse_events import MouseEvent, MouseEventType
from settings import Settings

class Configuration:
    jineng = 'yinyun-ziqi'
    # jineng = 'taoism'
    # jineng = 'force'
    shifu = 'song'
    xx = f"xue {shifu} {jineng} 20"

    def __init__(self, session) -> None:
        self.session = session
        self._aliases = {}
        self._triggers = {}
        self._commands = {}
        self._timers = {}

        self._initAliases()
        self._initTriggers()
        # self._initCommands()
        # self._initTimers()
        self.session.info('\x1b[1;36m=== 留学：武当、桃花 ===')
        
    # 状态栏
    def status_window(self):
            formatted_list = list()
            def dtFn(mouse_event: MouseEvent):
                if mouse_event.event_type == MouseEventType.MOUSE_UP:
                    # self.session.exec("inp")
                    self.session.exec("dt")
                    self._triggersSwitch("tri_wdlx")
                    self.jobType = 'typeDT'
            def pyFn(mouse_event: MouseEvent):
                if mouse_event.event_type == MouseEventType.MOUSE_UP:
                    self.session.exec("py")
                    self._triggersSwitch("tri_py")
                    self.jobType = 'typePY'
            def stopFn(mouse_event: MouseEvent):
                if mouse_event.event_type == MouseEventType.MOUSE_UP:
                    self.session.info('停下')
                    self._triggersSwitch('')
            def testFn(mouse_event: MouseEvent):
                if mouse_event.event_type == MouseEventType.MOUSE_UP:
                    self.session.info('【弹窗测试】')
            
            formatted_list.append((Settings.styles["datie"], "【打铁】", functools.partial(dtFn)))
            formatted_list.append((Settings.styles["peiyao"], "【配药】", functools.partial(pyFn)))
            formatted_list.append((Settings.styles["red"], "【弹窗测试】", functools.partial(testFn)))
            formatted_list.append((Settings.styles["stop"], "【停止所有机器】", functools.partial(stopFn)))
            return formatted_list
            
    # 别名
    def _initAliases(self):
        self._aliases["alia_guxu"] = SimpleAlias(self.session, 
            r"^xx$", self.xx, 
            group="alia_guxu"
        )
        self.session.addAliases(self._aliases)

    # 触发
    def _initTriggers(self):
        this = self.session
        self._triggers["tri_wdlx_0"] = SimpleTrigger(self.session,
            r"^你听了.+的指导，似乎有些心得。",
            f'#wa 2000;{self.xx}',
            group="tri_wdlx", id="tri_wdlx_0",
        )
        self._triggers["tri_wdlx_1"] = Trigger(self.session,
            r"^然而你今天太累了，无法再进行任何学习了。",
            group="tri_wdlx", id="tri_wdlx_1",
            onSuccess = self._onTriggerSleep
        )
        self._triggers["tri_wdlx_2"] = SimpleTrigger(self.session,
            r"^你一觉醒来，只觉精力充沛。该活动一下了。|你迷迷糊糊的睁开双眼，爬了起来。",
            f"s;w;w;{self.xx}",
            group="tri_wdlx", id="tri_wdlx_2"
        )
        
        this.status_maker = self.status_window
        this.addTriggers(self._triggers)

    # 定时器
    def _initTimers(self):
        self._timers["tim_food"] = self.timerFood = Timer(
            self.session, timeout=6, enabled=False,
            id="tim_food", group="timers",
            onSuccess = self.onTimerFood
        )
        self.session.addTimers(self._timers)


    ''' ========================================== '''
    ''' ==           Timers 自定义函数           == '''
    ''' ========================================== '''
    def onTimerFood(self, name, *args, **kwargs):
        self.timerFood.enabled = False
        self._goOnJob()
        self.jobCount = 0


    ''' ========================================== '''
    ''' ==          Trigger 自定义函数           == '''
    ''' ========================================== '''
    def _onTriggerSleep(self, name, line, wildcards):
        self.session.create_task(self._asyncSleep(wildcards))
    async def _asyncSleep(self, wildcards):
        await asyncio.sleep(10)
        self.session.writeline('e;e;n;sleep')

    
    ''' ========================================== '''
    ''' ==                公共函数               == '''
    ''' ========================================== '''